package com.lixun.passwordbook.custom;

import com.lixun.passwordbook.pojo.Colors;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import com.lixun.passwordbook.ResourceTable;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static com.lixun.passwordbook.slice.MainAbilitySlice.DIALOG_BOX_CORNER_RADIUS;
import static com.lixun.passwordbook.slice.MainAbilitySlice.DIALOG_BOX_CORNER_WIDTH;

/**
 * CustomDialog
 */
public class CustomDialog extends CommonDialog {
    private TextField password1;
    private TextField password2;
    private Text titleText;
    private Component confirmButton;
    private ConfirmListener confirmListener;
    private final Context context;

    /**
     * CustomDialog
     *
     * @param abilityContext Context
     */
    public CustomDialog(Context abilityContext) {
        super(abilityContext);
        this.context = abilityContext;
        initComponents();
        setCornerRadius(DIALOG_BOX_CORNER_RADIUS);
        setAlignment(TextAlignment.CENTER);
        setSize(DIALOG_BOX_CORNER_WIDTH, MATCH_CONTENT);
    }

    private void initComponents() {
        Component customComponent = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_custom_dialog_content, null, true);
        password1 = (TextField) customComponent.findComponentById(ResourceTable.Id_set_modify_password);
        password2 = (TextField) customComponent.findComponentById(ResourceTable.Id_set_confirm_password);
        titleText = (Text) customComponent.findComponentById(ResourceTable.Id_title_text);
        confirmButton = customComponent.findComponentById(ResourceTable.Id_confirm_button);
        super.setContentCustomComponent(customComponent);
        confirm();
    }

    /**
     * 设置标题
     *
     * @param string 需要设置的内容
     */
    public void setTitle(String string) {
        titleText.setText(string);
        titleText.setTextColor(Colors.RED);
    }


    private String getContent() {
        return password1.getText();
    }

    private void confirm() {
        confirmButton.setClickedListener(component -> {
            if (confirmListener != null) {
                String pwd1 = password1.getText();
                String pwd2 = password2.getText();
                String results;
                if (pwd1 == null || pwd1.isEmpty()){
                    results = "请输入密码";
                } else if(pwd1.equals(pwd2)){
                    results = "设置成功";
                    confirmListener.onConfirmListener(getContent());
                } else {
                    results = "确认密码与密码不相同，请重新输入。";
                }
                setTitle(results);
            }
        });
    }

    /**
     * setOnConfirmListener
     *
     * @param confirm ConfirmListener
     */
    public void setOnConfirmListener(ConfirmListener confirm) {
        confirmListener = confirm;
    }
}
